using NodeCanvas.Framework;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI.Node;

namespace com.yoozoo.gta
{
    public class NpcSeek : NavMeshMovement
    {
        public BBParameter<Vector3> targetPosition;
        public BBParameter<Transform> target;

        public BBParameter<float> errorDistance = 0.5f;
        public BBParameter<float> limitTime = -1;

        
        protected override void OnExecute()
        {
            if (target.value ==null && targetPosition.value == Vector3.zero)
            {
                EndAction(false);
                return;
            }
            SetDestination(Target());
            // _animator.SetBool("Run", true);
        }

        protected override void OnUpdate()
        {
            base.OnUpdate();
            Vector3 npcPosition = _entity.viewer.animator.transform.position;
            if (HasArrived() || Vector3.Distance(Target(), npcPosition)<=errorDistance.value)
            {
                //_animator.SetBool("Run", false);
                targetPosition.value = default;
                target.value = null;
                EndAction(true); 
            }
            else
            {
                if(limitTime.value > 0 && elapsedTime >= limitTime.value)
                {
                    // 追击时长限制
                    EndAction(false);
                }
            }
        }

        private Vector3 Target()
        {
            if (target.value != null) {
                return target.value.position;
            }
            return targetPosition.value;
        }
    }
}
